<template>
	<view>
		<view class="goods-list">
			<view class="goods-item" v-for="(item,index) in goodsList" :key="index" @click="goodsDetailAction(item)">
				<my-goods :goods = "item"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj: {
					query: '', //查询关键词 
					cid: '', //分类ID
					pagenum: 1,
					pagesize: 10,
				},
				goodsList: [],
				total: 0,
				isLoading: false
			};
		},
		
		onLoad(option) {
			this.queryObj.query = option.query || ''
			this.queryObj.cid = option.cid || ''
			
			this.getGoodslist()
		},
		
		methods: {
			async getGoodslist(func) {
				
				this.isLoading = true
				
				const {data: res} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
				
				this.isLoading = false
				
				func && func()
				
				if (res.meta.status !== 200) return uni.$showMsg()
				
				//console.log(res)
				
				this.goodsList = [...this.goodsList, ...res.message.goods]
				this.total = res.message.total
			},
			
			goodsDetailAction(goods) {
				uni.navigateTo({
					url: '../goods_detail/goods_detail?goods_id=' + goods.goods_id
				})
			}
		},
		
		//上拉加载更多
		onReachBottom() {
			
			if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕!')
			
			if (this.isLoading) return
			
			this.queryObj.pagenum++
			
			this.getGoodslist()
		},
		
		//下拉刷新
		onPullDownRefresh() {
	
			this.queryObj.pagenum = 1
			this.goodsList = []
			this.total = 0
			this.isLoading = false
			
			this.getGoodslist(()=> {
				uni.stopPullDownRefresh()
			})
		}
	}
</script>

<style lang="scss">

</style>
